//
//  InLocalCacheViewController.m
//  Inner  Voice
//
//  Created by brother on 16/3/10.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InLocalCacheViewController.h"

#import "InLocalCacheTableViewCell.h"

#import "AppDelegate.h"


#define kkIdentifier @"localCacheCell"

#import "InSound.h"

#import "InDownLoadSound.h"

@interface InLocalCacheViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (nonatomic,strong)NSMutableArray *soundArray;

@end

@implementation InLocalCacheViewController

- (NSMutableArray *)soundArray {
    if (_soundArray == nil) {
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:[InDownLoadSound sadBox]]) {
            _soundArray = [InDownLoadSound soundArray];
        }else {
            _soundArray = [NSMutableArray array];
            
        }
    }
    return _soundArray;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.myTableView.delegate = self;
    
    self.myTableView.dataSource = self;
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"InLocalCacheTableViewCell" bundle:nil] forCellReuseIdentifier:kkIdentifier];
    // Do any additional setup after loading the view from its nib.
}



- (IBAction)backBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (IBAction)startBtnClick:(id)sender {
    
    InPlayerViewController *contro = [[InPlayerViewController alloc]init];
    [self presentViewController:contro animated:YES completion:nil];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.soundArray.count;
   
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InLocalCacheTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kkIdentifier forIndexPath:indexPath];
    
    InSound *sound = self.soundArray[indexPath.row];
    
    cell.nameLabel.text = sound.inSound_name;
    
    [cell loadImageWithStr:sound.inSound_icon];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//删除按钮的标题

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"删除";
    
}

//这里就是点击删除执行的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    InSound *sound = self.soundArray[indexPath.row];
    
    BOOL success = [InDownLoadSound deleteSound:sound];
    if (success) {
        [self.soundArray removeObjectAtIndex:indexPath.row];
        
        //NSRange是结构体
        NSRange range = [sound.inSound_Url rangeOfString:@"file://"];
        
        NSString *subString = [sound.inSound_Url substringFromIndex:range.location + range.length];//筛选后得到的结果
        
     //   NSLog(@" sdsadsa subString   %@",subString);
        
        NSError *error = nil;
        
        [[NSFileManager defaultManager] removeItemAtPath:subString error:&error];
        
        [self.myTableView reloadData];
        
        NSLog(@"删除成功");
        
    }else {
        NSLog(@"删除失败");
    }
}

//点击后的事件没写
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    
    InSound *sound = self.soundArray[indexPath.row];//得到当前点击的歌曲的信息
    
    //播放本地文件要加file://
    NSURL *url = [NSURL URLWithString:sound.inSound_Url];
    
    [delegate.InPlayerViewController.inplayer playWithUrl:url showView:delegate.InPlayerViewController.sliderView];//播放该歌曲的信息
    
    delegate.InPlayerViewController.index = indexPath.row;//将歌曲的index传给播放器页面
    
    
    delegate.InPlayerViewController.inSound = sound;
    
    delegate.InPlayerViewController.playingSoundArray = self.soundArray;//播放器正在播放的数组为历史播放记录的数组
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
